
goods=[(60,10),(100,20),(120,30)] #每个商品元组表示（价格，重量）
goods.sort(key=lambda x: x[0] / x[1], reverse=True)  # 价格除以重量

def fractional_backpack(goods,w):
    m=[0 for _ in range(len(goods))]
    total_val=0
    for i,(prize,weight) in enumerate(goods):
        if w>=weight:
            m[i]=1
            w-=weight
            total_val+=prize
        else:
            m[i]=w/weight
            w=0
            total_val+=m[i]*prize
            break
    return m,total_val

print(fractional_backpack(goods,50))